<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑正面模板</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
</head>
<body style="height: 750px;background-color: white">
<div style="width:80%;height:100%;margin:15px auto;overflow:auto;" id="aaa">
    <form class="layui-form" action="">
        <input type="hidden" class="input-text" id="id" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">模板名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" required  lay-verify="required" placeholder="请输入模板名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">号牌号码X</label>
            <div class="layui-input-block">
                <input type="text" name="haopaiX" required  lay-verify="required|double" placeholder="请输入号牌号码X" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">号牌号码Y</label>
            <div class="layui-input-block">
                <input type="text" name="haopaiY" required  lay-verify="required|double" placeholder="请输入号牌号码Y" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车辆型号X</label>
            <div class="layui-input-block">
                <input type="text" name="clTypeX" required  lay-verify="required|double" placeholder="请输入车辆型号X" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车辆型号Y</label>
            <div class="layui-input-block">
                <input type="text" name="clTypeY" required  lay-verify="required|double" placeholder="请输入车辆型号Y" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所有人X</label>
            <div class="layui-input-block">
                <input type="text" name="nameX" required  lay-verify="required|double" placeholder="请输入所有人X" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所有人Y</label>
            <div class="layui-input-block">
                <input type="text" name="nameY" required  lay-verify="required|double" placeholder="请输入所有人Y" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">住址X</label>
            <div class="layui-input-block">
                <input type="text" name="addressX" required  lay-verify="required|double" placeholder="请输入住址X" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">住址Y</label>
            <div class="layui-input-block">
                <input type="text" name="addressY" required  lay-verify="required|double" placeholder="请输入住址Y" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">品牌型号X</label>
            <div class="layui-input-block">
                <input type="text" name="pinpaiTypeX" required  lay-verify="required|double" placeholder="请输入品牌型号X" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">品牌型号Y</label>
            <div class="layui-input-block">
                <input type="text" name="pinpaiTypeY" required  lay-verify="required|double" placeholder="请输入品牌型号Y" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">使用性质X</label>
            <div class="layui-input-block">
                <input type="text" name="useX" required  lay-verify="required|double" placeholder="请输入使用性质X" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">使用性质Y</label>
            <div class="layui-input-block">
                <input type="text" name="useY" required  lay-verify="required|double" placeholder="请输入使用性质Y" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车辆识别代码X</label>
            <div class="layui-input-block">
                <input type="text" name="clsbdyX" required  lay-verify="required|double" placeholder="请输入车辆识别代码X" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">车辆识别代码Y</label>
            <div class="layui-input-block">
                <input type="text" name="clsbdyY" required  lay-verify="required|double" placeholder="请输入车辆识别代码Y" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">发动机号码X</label>
            <div class="layui-input-block">
                <input type="text" name="fdjX" required  lay-verify="required|double" placeholder="请输入发动机号码X" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">发动机号码Y</label>
            <div class="layui-input-block">
                <input type="text" name="fdjY" required  lay-verify="required|double" placeholder="请输入发动机号码Y" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">注册时间X</label>
            <div class="layui-input-block">
                <input type="text" name="registerX" required  lay-verify="required|double" placeholder="请输入注册时间X" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">注册时间Y</label>
            <div class="layui-input-block">
                <input type="text" name="registerY" required  lay-verify="required|double" placeholder="请输入注册时间Y" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">发证时间X</label>
            <div class="layui-input-block">
                <input type="text" name="fzX" required  lay-verify="required|double" placeholder="请输入发证时间X" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">发证时间Y</label>
            <div class="layui-input-block">
                <input type="text" name="fzY" required  lay-verify="required|double" placeholder="请输入发证时间Y" autocomplete="off" class="layui-input">
            </div>
        </div>

        <!-- 图片上传 -->
        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test" style="display: inline;float: left;">上传背景图片</button>
            <div class="layui-upload-list" style="display: inline;margin-left: 30px;">
                <img class="layui-upload-img" name="logo" id="logo" style="width: 200px;height: 150px">
                <p id="demoText"></p>
            </div>
            <input type="hidden" class="input-text" id="picture" name="picture">
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="启用" checked=""><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i>启用</div>
                <input type="radio" name="status" value="0" title="禁用"><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>禁用</div>
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button class="layui-btn" id="addWuye" lay-submit lay-filter="addWuye">立即修改</button>
                <button type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/layui/layui.js"></script>
<script src="/js/layui/layui.all.js"></script>
<script src="/admin/js/token.js"></script>

<script>
    //url 传参
    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var a = decodeURI(window.location.search);
        var r = a.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
        if(r!=null)return  unescape(r[2]); return null;
    }
    window.onload = function(){

        var form = layui.form;

        $("input[name='id']").val(GetQueryString("id"));
        $("input[name='name']").val(GetQueryString("name"));
        $("input[name='picture']").val(GetQueryString("bg"));
        $("input[name='haopaiX']").val(GetQueryString("haopaiX"));
        $("input[name='haopaiY']").val(GetQueryString("haopaiY"));
        $("input[name='nameX']").val(GetQueryString("nameX"));
        $("input[name='nameY']").val(GetQueryString("nameY"));
        $("input[name='clTypeX']").val(GetQueryString("clTypeX"));
        $("input[name='clTypeY']").val(GetQueryString("clTypeY"));
        $("input[name='addressX']").val(GetQueryString("addressX"));
        $("input[name='addressY']").val(GetQueryString("addressY"));

        $("input[name='pinpaiTypeX']").val(GetQueryString("pinpaiTypeX"));
        $("input[name='pinpaiTypeY']").val(GetQueryString("pinpaiTypeY"));

        $("input[name='useX']").val(GetQueryString("useX"));
        $("input[name='useY']").val(GetQueryString("useY"));

        $("input[name='clsbdyX']").val(GetQueryString("clsbdyX"));
        $("input[name='clsbdyY']").val(GetQueryString("clsbdyY"));

        $("input[name='fdjX']").val(GetQueryString("fdjX"));
        $("input[name='fdjY']").val(GetQueryString("fdjY"));

        $("input[name='registerX']").val(GetQueryString("registerX"));
        $("input[name='registerY']").val(GetQueryString("registerY"));

        $("input[name='fzX']").val(GetQueryString("fzX"));
        $("input[name='fzY']").val(GetQueryString("fzY"));

        $('#logo').attr('src', "/upload/"+GetQueryString("bg"));

        var x = GetQueryString("status");
        if(x==1){
            $("#qiyong").prop("checked",true);
            form.render()
        }else{
            $("#jinyong").prop("checked",true);
            form.render()
        }
    }

    layui.use(['form' , 'jquery' , 'upload' , 'colorpicker'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;

        var colorpicker = layui.colorpicker;

        var cor = undefined
        //渲染
        colorpicker.render({
            elem: '#color'
            ,done: function(color){
                console.log(color)
                cor = color
                //譬如你可以在回调中把得到的 color 赋值给表单
            }
        });

        //自定义验证规则  检测是否为数字
        form.verify({
            double: function(value){
                if(/^\d+$/.test(value)==false && /^\d+\.\d+$/.test(value)==false)
                {
                    return '你输入的不是数字';
                }
            }
        });

        //编辑操作
        form.on('submit(addWuye)',function(data){
            var bg = $("input[name='picture']").val();
            data.field.bg=bg;
            data.field.txtColor = cor
            if(bg==null || bg==""){
                layer.msg('请上传背景图片！');
            }else{
                $.ajax({
                    url:"/mobanZm/updMobanZm"
                    ,type:"POST"
                    ,contentType: 'application/json'
                    ,data:JSON.stringify(data.field)
                    ,success:function (data) {
                        if(data.code==3){
                            layer.msg('请上传背景图片！');
                        }else if(data.code==2) {
                            layer.msg('模板名称已存在！请重新输入');
                        }else if(data.code==0){
                            layer.close(layer.index);
                            layer.msg('编辑成功');
                            setTimeout(function(){
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);//关闭弹出层
                                parent.location.reload();//重新加载父页面表格
                            }, 2000);
                        }else if(data.code==1){
                            layer.msg('编辑失败！');
                        }else{
                            layer.msg('拒绝访问');
                        }
                    }
                    ,error:function () {
                        console.log("ajax error");
                    }
                });
            }
            return false;
        });

        //bg上传
        var uploadInst = upload.render({
            elem: '#test'
            , url: '/upload' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#logo').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //判断上传是否成功
                if (res.code == 0) {
                    $("#picture").val(res.data);
                    return layer.msg('上传成功！');
                }else{
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
        // 设置下拉列表的高度
        $(".layui-form-select dl").css("max-height",'400px')
    });
</script>

</body>
</html>